// pages/setting/setting.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 管理袁入口开关
    'AdminEnterEnable': app.globalData.config.AdminEnterEnable
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '设置',
    })
  },
  // 跳转绑定账号
  toBind: function() {
    wx.navigateTo({
      url: '/pages/agent/center/bind/bind',
    })
  },

  // 注销登录
  bindToLogout: function() {
    var data = {
      point_url: app.server.agentApi.logout
    }
    app.server.getConnect(data).then(function(res) {
      wx.setStorageSync("userInfo", '');
      app.globalData.userInfo = '';
      wx.navigateBack();
      wx.showToast({
        title: '解绑成功',
      })
    })
  },

  // 跳转修改密码手机验证
  toMobileVerification: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/agent/center/mobileVerification/mobileVerification?pagetype=' + e.currentTarget.dataset.pagetype,
    })
  },
  // 跳转修改密码
  toModifyPassword: function(e) {
    wx.navigateTo({
      url: '/pages/agent/center/passwordModify/passwordModify?pagetype=' + e.currentTarget.dataset.pagetype,
    })
  },
  // 跳转关于我们页面
  toAboutUs: function() {
    wx.navigateTo({
      url: '/pages/agent/center/aboutUs/aboutUs',
    })
  },
  // 跳转服务协议页面
  toAgreement: function() {
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },
  // 跳转使用帮助
  toUseHelp: function() {
    wx.navigateTo({
      url: '/pages/agent/center/useHelp/useHelp',
    })
  },

  getAdminInfo: function() {
    var that = this;
    console.log(app.globalData)
    if (!app.globalData.openid) {
      wx.navigateTo({
        url: '/pages/admin/authorization/authorization',
      })
      return;
    }
    var data = {
      point_url: app.server.adminApi.getUsersInfo,
      openid: app.globalData.openid
    }
    app.server.getConnect(data).then(function(res) {
      if (!res.data) {
        wx.navigateTo({
          url: '/pages/admin/bind/bind',
        })
      } else {
        wx.setStorageSync("userInfo", res.data);
        app.globalData.userInfo = res.data;
        app.globalData.config['selectedTarbarType'] = 'Admin';
        app.utils.initAdminWxappStyle(app, that, ()=>{
          if(app.globalData.config['tabbarType']==='custom'){
            wx.reLaunch({
              url: app.globalData.config.SwitchTabToAdminPageUrl,
            })
          } else {
            wx.switchTab({
              url: app.globalData.config.SwitchTabToAdminPageUrl,
            })
          }
        });
      }
    }, function(res) {
      console.log(res);
      wx.navigateTo({
        url: '/pages/admin/bind/bind',
      })
    })
  },

  bindToAdmin() {
    this.getAdminInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  // 调试模式
  clickDebugCount: 0,
  bindToDebug() {
    this.clickDebugCount++;
    let enableDebug = wx.getStorageSync('enableDebug') || false;
    if (this.clickDebugCount === 10) {
      this.clickDebugCount = 0;
      wx.setEnableDebug({
        enableDebug: !enableDebug
      });
      wx.setStorageSync('enableDebug', !enableDebug)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
